﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		chooseProp = getProperty("serviceFactory").getBean("choosePropertyAdvice");
		timeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		sequenceProgramTask = getProperty("serviceFactory").getBean("sequenceProgramTask");
		sequenceStudentCourse = getProperty("serviceFactory").getBean("sequenceStudentCourse");
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		termId = event.getArg("TermID", event.getArg("TID"));
		
		/* 学生相关信息 */
		sql = "SELECT a.stu_id,a.cls_id,
					   a.sbj_direction,b.student_prop 
				FROM t_student a 
					 INNER JOIN t_student_status b ON b.stu_id = a.stu_id 
				WHERE a.stu_id = :studentId ";
		queryObj = new Query( datasource=application.dnsSlave ) ;
		queryObj.addParam( name="studentId", value=event.getArg("STU"), cfsqltype="cf_sql_varchar" );
		rs_student= queryObj.execute( sql=sql ).getResult();
		
		/* 学籍相关信息 */
		studentId = rs_student.stu_id;
		classId = rs_student.cls_id;
		studentDir = rs_student.sbj_direction;
		
		
		if ( mid( rs_student.student_prop, 1, 1) eq "1" ) {
			
			transaction {
			
			
				timeStamp = timeAdvice.getCurrentDatetimeString();
			
				
				/* 载入班级必修课课程计划 */
				sql = "SELECT 
							a.cid, a.cidx, a.sbj_direction 
						FROM t_plan a  
						WHERE 
							a.prop_id = '1' 
							AND 
							a.cls_id = :classId 
							AND 
							a.term_id = :termId 
							AND 
							(a.cidx > 'XXX' OR a.cidx < 'XXX') 
							AND 
							(a.cidx > '000' OR a.cidx < '000') ";
				queryObj = new Query( datasource=application.dnsMaster ) ;
				queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
				rs_plan= queryObj.execute( sql=sql ).getResult();
			
				for ( a=1; a LTE rs_plan.recordCount; a++ ) {
					
					courseId = rs_plan["cid"][a];
					courseIdx = rs_plan["cidx"][a];
					
					planDir = rs_plan["sbj_direction"][a];
					
					/* required Term, required Course, required string CourseIndex */
					sequenceProgramTask.setTermID(termId);
					sequenceProgramTask.setCourseID(courseId);
					sequenceProgramTask.setCourseIndex(courseIdx);
					taskId = sequenceProgramTask.getID();
					/* 如果课程不分方向 或 方向与学生方向相同 */
					if ( listFind(planDir, studentDir, "/") or planDir eq "0") {
						
						/* required Student, required Course, required Term, required string TaskType */
						sequenceStudentCourse.setStudentID(rs_student.stu_id);
						sequenceStudentCourse.setCourseID(courseId);
						sequenceStudentCourse.setTermID(termId);
						sequenceStudentCourse.setTaskType("P");
						chooserId = sequenceStudentCourse.getID();
						
						sql = "SELECT a.choose_prop 
								FROM t_student_course a 
								WHERE a.scc_id = :chooserId ";
						queryObj = new Query( datasource=application.dnsMaster ) ;
						queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
						rs_studentCourse= queryObj.execute( sql=sql ).getResult();
				
						if ( rs_studentCourse.recordCount EQ 0 ) {
						
							prop = chooseProp.init();
							
							prop.setLocked(true);
							
							prop.setPrePaid( true ); /* 是否从预交学费扣除 */
							prop.setPaid( true ); /* 是否已收费 */
						
							/* 没有选课记录 */
							sql = "INSERT INTO t_student_course a (
										   a.scc_id,
										   a.task_type,
										   a.cidx,
										   a.task_id,
										   a.st,
										   a.retaking,
										   a.choose_time,
										   a.charge_time,
										   a.choose_prop,
										   a.prop_id,
										   a.term_id,
										   a.cid,
										   a.ct,
										   a.stu_id 
									) VALUES (
										    :ChooseID ,
											:TaskType ,
											:CourseIndex ,
											:TaskID ,
											:StudyType ,
											:Retaking ,
											:ChooseTime ,
											:ChargeTime ,
											:ChooseProp ,
											:CourseProperty ,
											:Term ,
											:Course ,
											:ChooseTurn ,
											:Student   
									)";
							queryObj = new Query( datasource=application.dnsMaster ) ;
							queryObj.addParam( name="ChooseID", value=chooserId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="TaskType", value="P", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="CourseIndex", value=courseIdx, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="TaskID", value=taskId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="StudyType", value="N", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="Retaking", value="0", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChooseTime", value=timeStamp, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChargeTime", value=timeStamp, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="CourseProperty", value="1", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="Term", value=termId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="Course", value=courseId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="ChooseTurn", value="9", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="Student", value=rs_student.stu_id, cfsqltype="cf_sql_varchar" );
							queryObj.execute( sql=sql );
							
						}
						else {
						
							/* 更新选课记录 */
							prop = chooseProp.init();
							prop.parseProperty( rs_studentCourse.choose_prop );
							prop.setLocked(true);
							
							prop.setPrePaid( true ); /* 是否从预交学费扣除 */
							prop.setPaid( true ); /* 是否已收费 */
						
							/*Unique: STU_ID, CID, TERM_ID, TASK_TYPE*/
							
							sql = "UPDATE t_student_course a 
									SET a.choose_prop = :ChooseProp ,
										a.cidx = :CourseIndex ,
										a.task_id = :TaskID ,
										a.st = :StudyType ,
										a.retaking = :Retaking ,
										a.choose_time = :ChooseTime ,
										a.charge_time = :ChargeTime ,
										a.ct = :ChooseTurn ,
										a.prop_id = :CourseProperty 
									WHERE a.scc_id = :ChooseID ";
							queryObj = new Query( datasource=application.dnsMaster ) ;
							queryObj.addParam( name="ChooseID", value=chooserId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="CourseIndex", value=courseIdx, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="TaskID", value=taskId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="StudyType", value="N", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="Retaking", value="0", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChooseTime", value=timeStamp, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChargeTime", value=timeStamp, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="CourseProperty", value="1", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="ChooseTurn", value="9", cfsqltype="cf_sql_char" );
							queryObj.execute( sql=sql );
						}
				
					}
					
					/* 完成一个课程后 刷新数据记录 */
					sql = "SELECT a.tsk_id
							FROM t_task a
							WHERE a.tsk_id = :taskId ";
					queryObj = new Query( datasource=application.dnsMaster ) ;
					queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
					rs_task= queryObj.execute( sql=sql ).getResult();
						
					if (rs_task.recordCount GT 0 ) {
						
						/* 每完成一批班级学生添加 刷新一次教学班已选人数 */
						sql = "SELECT count(*) num 
								FROM t_student_course a  
								WHERE a.task_id = :taskId ";
						queryObj = new Query( datasource=application.dnsMaster ) ;
						queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
						rs_taskCount= queryObj.execute( sql=sql ).getResult();
						
						if ( rs_taskCount.recordCount ) {
							sql = "UPDATE t_task a 
									SET 
										a.task_chooesd = :RealSize 
									WHERE a.tsk_id = :taskId ";
							queryObj = new Query( datasource=application.dnsMaster ) ;
							queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="RealSize", value=rs_taskCount.num, cfsqltype="cf_sql_integer" );
							queryObj.execute( sql=sql );
						}
					}

				}
			
			}
			structInsert(response, "success", true, true);
		}
		else {
			structInsert(response, "reason", "学生学籍标注为不在校, 不可待选必修课", true);
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>